package com.store.practice.controller;

import com.store.practice.common.api.CommonPage;
import com.store.practice.common.api.CommonResult;
import com.store.practice.model.BgPosition;
import com.store.practice.model.BgSkillTag;
import com.store.practice.service.BgPositionService;
import com.store.practice.service.BgSkillTagService;
import com.store.practice.service.BgStaffService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "BgSettingsController",description = "后台设置管理")
@RequestMapping("/bgSettings")
@RestController
public class BgSettingsController {
    @Autowired
    private BgSkillTagService tagService;
    @Autowired
    private BgPositionService positionService;
    @Autowired
    private BgStaffService staffService;

    @ApiOperation("展示技能标签列表")
    @GetMapping("/listTags")
    public CommonResult<CommonPage<BgSkillTag>> listTags(
            @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5") int pageSize){
        List<BgSkillTag> list = tagService.list(pageNum,pageSize);
        if (list==null){
            return CommonResult.failed();
        }
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("新增技能标签")
    @PostMapping("/insertTag")
    public CommonResult insertTag(@RequestBody BgSkillTag tag){
        int i = tagService.insert(tag);
        if (i>0){
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除技能标签")
    @PostMapping("/deleteTag")
    public CommonResult deleteTag(Integer id){
        int i = tagService.delete(id);
        if (i>0){
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改技能标签")
    @PostMapping("/updateTag")
    public CommonResult updateTag(BgSkillTag tag){
        int i = tagService.update(tag);
        if (i>0){
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @ApiOperation("展示职位列表")
    @GetMapping("/listPosition")
    public CommonResult<CommonPage<BgPosition>> listPosition(
            @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5") int pageSize){
        List<BgPosition> list = positionService.list(pageNum,pageSize);
        if (list==null){
            return CommonResult.failed();
        }
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("新增职位信息")
    @PostMapping("/insertPosition")
    public CommonResult insertPosition(Integer dptId,@RequestBody BgPosition position){
        int i = positionService.insertPosition(dptId, position);
        if (i>0){
            return CommonResult.success(i);
        }
        if (i==-1){
            return CommonResult.failed("该部门不存在");
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改职位信息")
    @PostMapping("/updatePosition")
    public CommonResult updatePosition(@RequestBody BgPosition position){
        int i = positionService.updatePosition(position);
        if (i>0){
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除职位信息")
    @PostMapping("/deletePosition")
    public CommonResult deletePosition(Integer id){
        int i = positionService.deletePosition(id);
        if (i==1){
            return CommonResult.success(i);
        }else if (i==10){
            return CommonResult.failed("该职位上有员工，无法删除");
        }
        return CommonResult.failed();
    }
}
